interface cookieType {
    setCookie: Function,
    getCookie: Function,
    getAllCookie: Function,
    removeCookie:Function
}

export let cookie: cookieType = {
    setCookie: (name: string, value: string, expires: number) => {
        document.cookie = `${name.trim()}=${value.trim()};expires=${expires}`
    },
    getCookie: (name: string) => {
        let cookiesList: Array<any | object> = []
        document.cookie.split(';').forEach((item: any, index) => {
            let idx: number = item.indexOf('=')
            let itemObj: {
                name: string,
                value: string
            }
            itemObj = {
                name: item.substring(0, idx).trim(),
                value: item.substring(idx + 1, item.length)
            }
            cookiesList.push(itemObj)
        })
        let idx = cookiesList.findIndex((item: { name: string, value: string }) => {
            return item.name == name
        })
        return cookiesList[idx]
    },
    getAllCookie: () => {
        let cookiesList: Array<any | object> = []
        document.cookie.split(';').forEach((item: any, index) => {
            let idx: number = item.indexOf('=')
            let itemObj: object = {
                name: item.substring(0, idx),
                value: item.substring(idx + 1, item.length)
            }
            cookiesList.push(itemObj)
        })
        return cookiesList
    },
    removeCookie:(name:string)=>{
        document.cookie = `${name}=;max-age=-1`
    }
}
